#include <MCP9808.h>


MCP9808 mcp1 = MCP9808(0x18);
MCP9808 mcp2 = MCP9808(0x1F);

static const int NUM_SAMPLES = 16;
float samples1[NUM_SAMPLES];
float samples2[NUM_SAMPLES];
int sample_index1 = 0;
int sample_index2 = 0;

void setup()
{
    Serial.begin(115200);
    Serial.println("MCP9808 test");
    while(! mcp1.begin() && mcp2.begin())
    {
        Serial.println("MCP9808 not found");
        delay(500);
    }
    mcp1.setResolution(MCP9808_SLOWEST);
    mcp2.setResolution(MCP9808_SLOWEST);
    Serial.println("MCP9808 OK");

    for (int i = 0; i < NUM_SAMPLES; i++) {
      samples1[i] = mcp1.getTemperature();
      samples2[i] = mcp2.getTemperature();
    delay(250);
    }

}

void loop()
{
    float temp_c1 = mcp1.getTemperature();
    samples1[sample_index1++] = temp_c1;
    if (sample_index1 == NUM_SAMPLES) sample_index1 = 0;
    float temp_c_avg1 = get_average1();

    float temp_c2 = mcp2.getTemperature();
    samples2[sample_index2++] = temp_c2;
    if (sample_index2 == NUM_SAMPLES) sample_index2 = 0;
    float temp_c_avg2 = get_average2();
    Serial.print("Temp1:");
    Serial.print(temp_c_avg1, 2);
    Serial.print("   Temp2:");
    Serial.print(temp_c_avg2, 2);
    Serial.print("   Delta:");
    Serial.println((temp_c_avg1-temp_c_avg2), 2);
    delay(250*NUM_SAMPLES);    

}

float get_average1() {
  float sum = 0;
  for (int i = 0; i < NUM_SAMPLES; i++) {
    sum += samples1[i];
  }
  return sum / NUM_SAMPLES;
}

float get_average2() {
  float sum = 0;
  for (int i = 0; i < NUM_SAMPLES; i++) {
    sum += samples2[i];
  }
  return sum / NUM_SAMPLES;
}
